package server_commands;

import static server.RLM_Server.activeConnections;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.StringTokenizer;
import java.util.Vector;

import server.ServerSocketConnection;
import connection_modul.DatabaseConnection;
/**
 * Potklasa klase Command koja je namenjena procesuiranju 
 * 
 * @author Uros Krkic & Martin Jondzic
 *
 */
public class CHNICKCommand extends Command {

	
	public CHNICKCommand(String parameter) {
		setCode(CHNICK_CMD);
		setParameter(parameter);
	}
	
	/**
	 * Vrsi se upis novog nickname-a korisnika koji je izrvsio promenu. Takodje, vrsi se
	 * obavestenje svih korisnika sa njegove liste, koji nisu nisu blokirani i nisu offline.
	 * Offline su korisnici su oni korisnici ciji je status u bazi offline ili 
	 * oni ciji se objekat ServerSocketConnection ne nalazi u vektoru aktivnih konekcija.
	 * 
	 * @param connection - konekcija ka klijentu
	 * @param dbConnection - objekat za rad sa bazom
	 */
	public boolean executeCommand(ServerSocketConnection connection, DatabaseConnection dbConnection) {		

		StringTokenizer tokenizer = new StringTokenizer(getParameter());
		
		String username = tokenizer.nextToken();
		String newNickname = tokenizer.nextToken();
		
		try {
			dbConnection.doUpdateStatement("UPDATE tblUsers SET tblUsers.nickname = '" + newNickname + "'" +
										   " WHERE tblUsers.username = '" + username + "'");
		

			// Svi korisnici koje treba obavestiti o promeni nick-a odredjenog korisnika
		
			String SQLSelectQuery = "SELECT tblContacts.user_dest " +
			"FROM tblContacts " +
			"WHERE  tblContacts.user_source = '" + username + "' AND tblContacts.user_dest NOT IN " +
			"(SELECT tblBlocks.user_dest FROM tblBlocks WHERE tblBlocks.user_source = '" + username + "');";
			
			dbConnection.doStatement(SQLSelectQuery);
		
			
			connection.setMessage("100 Nick has changed");
			
			ArrayList array = (ArrayList)dbConnection.data.clone();
			
			if(array.size() == 0) {
				dbConnection.doCommit();
				return true;
			}
				
			String message = "SERV_CHNICK " + username + " " + newNickname;
			
			// Obavestava sve korisnike o logovanju aktuelnog korisnika
			
			Iterator usersIterator = array.iterator();
			
			while ( usersIterator.hasNext() ) {
				
				// username korisnika 
				String user = ((Vector)usersIterator.next()).get(0).toString();
				
				ServerSocketConnection tempConnection = activeConnections.get(user);
				
				if (tempConnection != null) {
					tempConnection.setMessage(message);
//					System.out.println("Korisnik koji se obavestava o promeni nicka novog usera: " + user);
				}
			}
			
			System.out.println("--> User " + username + " has just changed the nickname!");
		
		} catch (SQLException e) {			
			e.printStackTrace();
			dbConnection.doRollBack();
			return false;
		}
		
		dbConnection.doCommit();
		
		return true;
	}
	
}
